package com.fy.utilsproject.utils.ziputil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * zip压缩和解压工具
 */
public class ZipUtil {
//参考：https://blog.csdn.net/Utzw0p0985/article/details/90481441

    /**
     * 计算压缩包大小
     *
     * @param file 压缩包文件
     * @return 返回压缩包大小，未验证，不知道返回的单位是什么
     */
    public static long getZipFIleSize(File file) {
        long size = 0;
        try {
            ZipFile zipFile = new ZipFile(file);
            Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
            while (enumeration.hasMoreElements()) {
                size += enumeration.nextElement().getSize();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 计算压缩包大小
     *
     * @param filePath 压缩包文件绝对路径
     * @return 返回压缩包大小，未验证，不知道返回的单位是什么
     */
    public static long getZipFIleSize(String filePath) {
        long size = 0;
        try {
            ZipFile zipFile = new ZipFile(filePath);
            Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
            while (enumeration.hasMoreElements()) {
                size += enumeration.nextElement().getSize();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return size;
    }

    public static void unZip(String inPath, String outPath) {
        try {
            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(inPath));
            ZipEntry zipEntry;
            String pathFileName = "";
            while ((zipEntry = zipInputStream.getNextEntry()) != null){
                //获取到当层名称
                pathFileName = zipEntry.getName();
                String currentPathFileName = "";
                if (zipEntry.isDirectory()){
                    //如果压缩包中的第一层是文件夹则创建文件夹
                    pathFileName = pathFileName.substring(0,pathFileName.length()-1);//这里为什么要把文件名最后一个去掉？
                    currentPathFileName = outPath+File.separator+pathFileName;
                    //noinspection ResultOfMethodCallIgnored
                    new File(currentPathFileName).mkdir();
                }else {
                    File file = new File(outPath+"/"+pathFileName);
                    if (!file.exists()){
                        //如果文件不存在则创建文件
                        file.getParentFile().mkdirs();//创建文件所在的目录，包括任何必要的但是不存在的父目录
                        //noinspection ResultOfMethodCallIgnored
                        file.createNewFile();
                    }
                    FileOutputStream outputStream = new FileOutputStream(file);
                    int len;
                    long count = 0;
                    byte[] buffer = new byte[1024];
                    while ((len = zipInputStream.read(buffer))!=-1){
                        count += len;
//                        final int progress = count*100/inPathFileSize;//计算进度
                        outputStream.write(buffer,0,len);
                        outputStream.flush();
                    }
                    outputStream.close();

                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
